package jmine.tec.web.diagnostician;

import static bancosys.tec.rtm.diagnostician.AbstractDiagnostician.JMX_DEFAULT_CACHE_DURATION_IN_SECONDS;
import static bancosys.tec.rtm.diagnostician.AbstractDiagnostician.JMX_NAMESPACE;
import static jmine.tec.web.diagnostician.WebAccessTimeDiagnostician.NAME;

import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedResource;

import jmine.tec.utils.date.ChronoBean;
import bancosys.tec.rtm.diagnostician.AbstractDiagnostician;
import bancosys.tec.rtm.diagnostician.DiagnosisBean;

/**
 * Diagnosticador que diz tempos de acesso à páginas Web.
 * 
 * @author Marcio Ribeiro (mmr)
 * @created Dec 9, 2011
 */
@ManagedResource(description = NAME, objectName = JMX_NAMESPACE + ":name=webAccessTime", currencyTimeLimit = JMX_DEFAULT_CACHE_DURATION_IN_SECONDS)
public class WebAccessTimeDiagnostician extends AbstractDiagnostician {

    public static final String NAME = "Tempo de acesso à páginas Web";

    private static final String KEY_MIN_TIME = "minTime";

    private static final String KEY_MAX_TIME = "maxTime";

    private static final String KEY_AVG_TIME = "avgTime";

    private static final String KEY_REQUESTS = "requests";

    private ChronoBean timeCounter;

    /**
     * @return contador de tempo.
     */
    public ChronoBean getTimeCounter() {
        return timeCounter;
    }

    /**
     * @param timeCounter contador de tempo.
     */
    public void setTimeCounter(ChronoBean timeCounter) {
        this.timeCounter = timeCounter;
    }

    /**
     * @return NAME.
     */
    public String getName() {
        return NAME;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean fillDiagnosisBean(DiagnosisBean bean) throws Exception {
        bean.addDescription("Avg time: " + this.timeCounter.getAvgTime() + "ms");
        bean.addDescription("Max time: " + this.timeCounter.getMaxTime() + "ms");
        bean.addDescription("Min time: " + this.timeCounter.getMinTime() + "ms");
        bean.addDescription("Requests: " + this.timeCounter.getRequests());

        bean.putValue(KEY_AVG_TIME, this.timeCounter.getAvgTime());
        bean.putValue(KEY_MAX_TIME, this.timeCounter.getMaxTime());
        bean.putValue(KEY_MIN_TIME, this.timeCounter.getMinTime());
        bean.putValue(KEY_REQUESTS, this.timeCounter.getRequests());
        return true;
    }

    /**
     * @return average request time.
     */
    @ManagedAttribute(description = KEY_AVG_TIME)
    public double getAverageTime() {
        return (Double) this.getValue(KEY_AVG_TIME);
    }

    /**
     * @return maximum time for a request.
     */
    @ManagedAttribute(description = KEY_MAX_TIME)
    public long getMaxTime() {
        return (Long) this.getValue(KEY_MAX_TIME);
    }

    /**
     * @return min time for a request.
     */
    @ManagedAttribute(description = KEY_MIN_TIME)
    public long getMinTime() {
        return (Long) this.getValue(KEY_MIN_TIME);
    }

    /**
     * @return min time for a request.
     */
    @ManagedAttribute(description = KEY_REQUESTS)
    public int getRequests() {
        return (Integer) this.getValue(KEY_REQUESTS);
    }

}
